


interface CutType {
    /** 'none': 不压缩图片, screen:按照屏幕压缩, auto:根据图片尺寸压缩 */
    cut?: 'none' | 'screen' | 'auto';
    width?: number;
    height?: number;
    quality?: number;
}


/** 计算宽高 */
const setSize = (imgWidth: number, imgHeight: number, defineWidth: number, defineHeight: number): any => {
    const result = {
        width: 1,
        height: 1
    };
    const imgRatio: number = imgWidth / imgHeight;
    const defineRatio: number = defineWidth / defineHeight;
    /* 图片的宽高都小于 屏幕的宽高。取图片的宽高 */
    if (imgWidth <= defineWidth && imgHeight <= defineHeight) {
        result.width = imgWidth;
        result.height = imgHeight;
    } else {
        if (imgRatio >= defineRatio) {
            /* 图片极限 扁 */
            result.width = defineWidth;
            result.height = defineWidth / imgRatio;
        } else {
            /* 图片极限 长 */
            result.height = defineHeight;
            result.width = defineHeight * imgRatio;
        }
    }
    return result;
}

/** 图片处理工具 */
export class ImageUtils {
    /** 错误图片 */
    public static errorImage = "";


    /** src 转换 base64 */
    public static srcToBase64(src: string, type: CutType = {}): Promise<string> {
        const defaultType: CutType = { cut: 'auto', width: 1000, height: 1000, quality: 0.8 };
        const cutType = { ...defaultType, ...type };
        // const list = {
        //     jpg: 'image/jpeg',
        //     png: 'image/png',
        //     gif: 'image/gif',
        //     bmp: 'image/bmp'
        // };
        // const imgType = src.replace(/.+\.(jpg|png|gif|bmp)/i, '$1').toLowerCase();
        return new Promise((resolve) => {
            const img = new Image();
            /** 支持图片跨域 */
            img.crossOrigin = "Anonymous";
            img.src = src;

            img.onload = () => {
                let width: number = 1;
                let height: number = 1;
                const imgWidth: number = img.width;
                const imgHeight: number = img.height;

                switch (cutType.cut) {
                    case 'none':
                        width = img.width;
                        height = img.height;
                        break;
                    case 'screen':
                        const windowWidth = window.screen.width;
                        const windowHeight = window.screen.height;
                        const result1 = setSize(imgWidth, imgHeight, windowWidth, windowHeight);
                        width = result1.width;
                        height = result1.height;
                        break;
                    case 'auto':
                        const defineWidth = cutType.width!;
                        const defineHeight = cutType.height!;
                        const result2 = setSize(imgWidth, imgHeight, defineWidth, defineHeight);
                        width = result2.width;
                        height = result2.height;
                };
                const canvas = document.createElement("canvas");
                canvas.width = width;
                canvas.height = height;
                const ctx = canvas.getContext("2d");
                if (ctx) {
                    ctx.drawImage(img, 0, 0, width, height);
                    const base64 = canvas.toDataURL('image/jpeg', cutType.quality);
                    resolve(base64);
                } else {
                    resolve(ImageUtils.errorImage);
                }
            };
            img.onerror = () => {
                resolve(ImageUtils.errorImage);
            };
            img.src = src;

        });
    }

    /** image file 转换 base64 */
    public static imageFileToBase64(file: File, type: CutType = {}): Promise<string> {
        const defaultType: CutType = { cut: 'auto', width: 1000, height: 1000, quality: 0.8 };
        const cutType = { ...defaultType, ...type };
        const src = URL.createObjectURL(file);
        return new Promise((resolve) => {
            ImageUtils.srcToBase64(src, cutType).then((base64) => {
                resolve(base64);
            });
        });
    }

    /** 是否是图片文件 */
    public static isImageFile(file: File): boolean {
        // 检查文件类型是否以'image/'开头
        return file.type.startsWith('image/');
    }

    /** 是否是图片文件名 */
    public static isImageFileName(fileName: string): boolean {
        // 检查文件名是否以'.jpg'、'.jpeg'、'.png'、'.gif'、'.bmp'结尾
        const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp'];
        return imageExtensions.some(ext => fileName.toLowerCase().endsWith(ext));
    }
}